package com.gugee.ins.data.blogger.statistics.core;


import com.gugee.ins.common.dal.service.blogger.InsRelBloggerFollowerDao;
import com.gugee.ins.common.dal.service.statistics.InsStBloggerRealFollowingDao;
import com.gugee.ins.common.dto.InsBloggerFollowerTypeDto;
import com.gugee.ins.common.model.mysql.blogger.InsBloggerMain;
import com.gugee.ins.common.model.mysql.statistics.InsStBloggerRealFollowing;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import com.xiguaji.boot.toolkit.ThreadPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * 真实粉实统计
 */
@Service
@Slf4j
public class StBloggerRealFollowerService extends StBaseService{
    @Autowired
    private InsRelBloggerFollowerDao insRelBloggerFollowerDao;

    @Autowired
    private InsStBloggerRealFollowingDao insStBloggerRealFollowingDao;

    @Autowired
    @Qualifier("insBloggerMsgBaseInfoProducer")
    private KafkaTemplate<Long, InsBloggerMessage> insBloggerMsgBaseInfoProducer;

    String threadPoolName = "BloggerRealFollower-ThreadPool";

    @PostConstruct
    public void init() {
        forkJoinPool = ThreadPoolUtils.forkJoinPool(bloggerStatisticsAppConfig.getBloggerRealFollowerThreadPoolSize(), threadPoolName);
        scheduleTemplate.attach(threadPoolName, forkJoinPool);
        workName = "BloggerRealFollower";
    }

    /**
     * 统计真实粉丝的数据
     * @param insBloggerMessage
     */
    @Override
    protected void statistics(InsBloggerMessage insBloggerMessage){
        // 样本数,可疑账户数，大量粉丝数
        InsBloggerFollowerTypeDto insBloggerFollowerTypeDto = insStBloggerRealFollowingDao.findFollowerAndZombieByUid(insBloggerMessage.getUid());
        // 如果样本数为0就推出
        if(insBloggerFollowerTypeDto.getTotal() < 1){
            return;
        }
        // 获取粉丝数
        InsBloggerMain insBloggerMain = bloggerMain(insBloggerMessage.getUid());
        // 计算大量关注者的占比 * 1000
        long massiveFollowingRate = insBloggerFollowerTypeDto.getMassiveFollowing() * 1000L / insBloggerFollowerTypeDto.getTotal();
        // 计算可疑账户的占比 * 1000
        long suspiciousRate = insBloggerFollowerTypeDto.getSuspicious() * 1000L / insBloggerFollowerTypeDto.getTotal();
        //真实粉粉丝数占比
        double realFollowerRate = (double)(1000 - massiveFollowingRate - suspiciousRate) / 1000;

        //真实粉丝数
        long resultRealFollowerCount = Math.round(insBloggerMain.getFollowerCount() * realFollowerRate);

        // 保存主表信息
        insBloggerDao.updateRealFollowerRate(insBloggerMessage.getUid(),realFollowerRate);
        insBloggerMainDao.updateRealFollowerRate(insBloggerMain.getId(),realFollowerRate);

        // 设置统计信息
        InsStBloggerRealFollowing insStBloggerRealFollowing = insStBloggerRealFollowingDao.findByUid(insBloggerMessage.getUid());

        if(null == insStBloggerRealFollowing){
            insStBloggerRealFollowing = new InsStBloggerRealFollowing();
            insStBloggerRealFollowing.setUid(insBloggerMessage.getUid());
            insStBloggerRealFollowing.setCreateTime(System.currentTimeMillis()/1000);
        }
        insStBloggerRealFollowing.setOverallCategoryName(insBloggerMain.getOverallCategoryName());
        insStBloggerRealFollowing.setCategoryName(insBloggerMain.getCategoryName());
        insStBloggerRealFollowing.setFollowerSize(insBloggerMain.getFollowerSize());
        insStBloggerRealFollowing.setFollowerCount(insBloggerMain.getFollowerCount());
        insStBloggerRealFollowing.setRealFollowerCount(resultRealFollowerCount);
        insStBloggerRealFollowing.setRealFollowerSampleCount(insBloggerFollowerTypeDto.getTotal());
        insStBloggerRealFollowing.setRealFollowerRate(realFollowerRate);
        insStBloggerRealFollowing.setSuspiciousRate((double)suspiciousRate / 1000);
        insStBloggerRealFollowing.setMassiveFollowingRate((double)massiveFollowingRate / 1000);
        insStBloggerRealFollowingDao.saveOrUpdate(insStBloggerRealFollowing);

        // 发送kafka
        InsBloggerMessage insBloggerMessageForEs = new InsBloggerMessage();
        insBloggerMessageForEs.setId(insBloggerMain.getId());
        insBloggerMessageForEs.setUid(insBloggerMain.getUid());
        insBloggerMsgBaseInfoProducer.send(bloggerStatisticsAppConfig.getInsMBloggerRealFollowerToEsTopic(),insBloggerMessageForEs);

    }
}
